package com.pig4cloud.pig.model.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.model.api.entity.ModelServiceEntity;
import com.pig4cloud.pig.model.service.ModelServiceService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 统一服务表
 *
 * @author wangyl
 * @date 2024-06-06 10:53:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/modelService" )
@Tag(description = "modelService" , name = "统一服务表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ModelServiceController {

    private final  ModelServiceService modelServiceService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param modelService 统一服务表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('model_modelService_view')" )
    public R getModelServicePage(@ParameterObject Page page, @ParameterObject ModelServiceEntity modelService) {
    	LambdaQueryWrapper<ModelServiceEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(modelService.getName()),ModelServiceEntity::getName,modelService.getName());
        return R.ok(modelServiceService.page(page, wrapper));
    }

    /**
     * 通过id查询统一服务表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('model_modelService_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(modelServiceService.getById(id));
    }

    /**
     * 新增统一服务表
     * @param modelService 统一服务表
     * @return R
     */
    @Operation(summary = "新增统一服务表" , description = "新增统一服务表" )
    @SysLog("新增统一服务表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('model_modelService_add')" )
    public R save(@RequestBody ModelServiceEntity modelService) {
        return R.ok(modelServiceService.save(modelService));
    }

    /**
     * 修改统一服务表
     * @param modelService 统一服务表
     * @return R
     */
    @Operation(summary = "修改统一服务表" , description = "修改统一服务表" )
    @SysLog("修改统一服务表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('model_modelService_edit')" )
    public R updateById(@RequestBody ModelServiceEntity modelService) {
        return R.ok(modelServiceService.updateById(modelService));
    }

    /**
     * 通过id删除统一服务表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除统一服务表" , description = "通过id删除统一服务表" )
    @SysLog("通过id删除统一服务表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('model_modelService_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(modelServiceService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param modelService 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('model_modelService_export')" )
    public List<ModelServiceEntity> export(ModelServiceEntity modelService,Long[] ids) {
        return modelServiceService.list(Wrappers.lambdaQuery(modelService).in(ArrayUtil.isNotEmpty(ids), ModelServiceEntity::getId, ids));
    }
}
